package com.smfx.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.BaseQueryVO;
import com.smfx.yxt.reqVo.StudentReqVO;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.utils.DateUtil;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.view.StudentInfoView;
import com.smfx.yxt.view.TeacherInfoView;
import com.smfx.yxt.vo.StudentInfoInputVO;

/**
 * 学籍管理控制器
 * @author huqimeng
 * @date 2017年5月10日
 */

@RequestMapping(Const.TEACHER_ROLE + MenuConst.STUDENTS_MANAGE)
@Controller
public class StudentsManageController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(StudentsManageController.class);
	
	@Autowired
	private IStudentService studentService;
	@Autowired
	private ITeacherService teacherService;
	
	/**
	 * 学籍管理 如果是班主任设置班级
	 * @param model
	 * @return
	 */
	@RequestMapping(value={"","/"})
	public String studentList(Model model) {
		try {
			AppUser appUser = getAppUser();
			BaseQueryVO vo = new BaseQueryVO();
			vo.setFinishYear(getCurrentTerm().getFinishYear());
			vo.setId(appUser.getTeacher().getId());
			List<TeacherInfoView> teacherInfoView = teacherService.selectClassForTeacher(vo);
			if(!StringUtil.isNullOrEmpty(teacherInfoView)) {
				model.addAttribute("model", teacherInfoView.get(0));
			}
		} catch (Exception e) {
			logger.error("----studentList error={}", e);
		}
		return "teacher/studentsList";
	}

	/**
	 * 分页查询教师列表
	 * @param vo
	 * @return
	 */
	@RequestMapping("page/query")
	@ResponseBody Object pageQuery(StudentReqVO vo) {
		try {
			vo.setFinishYear(getCurrentTerm().getFinishYear());
			Page<StudentInfoView> page = new Page<StudentInfoView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("年级号", "desc"));
			sorts.add(new OrderBy("班号"));
			sorts.add(new OrderBy("姓名"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<StudentInfoView> list = studentService.selectByPageForBind(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("--pageQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping("keyword")
	@ResponseBody Object keywordQuery(StudentReqVO reqVO) {
		AppUser appUser = getAppUser();
		reqVO.setStudyType(appUser.getStudyType());
		reqVO.setFinishYear(getCurrentTerm().getFinishYear());
		reqVO.setSchoolId(appUser.getTeacher().getSchoolId());
		List<StudentInfoView> list = studentService.selectByKeywordForBind(reqVO);
		return list;
	}
	
	@RequestMapping("keyword1")
	@ResponseBody Object keywordQuery1(StudentReqVO reqVO) {
		AppUser appUser = getAppUser();
		reqVO.setFinishYear(getCurrentTerm().getFinishYear());
		reqVO.setSchoolId(appUser.getTeacher().getSchoolId());
		List<StudentInfoView> list = studentService.selectByKeyword(reqVO);
		return list;
	}
	
	/**
	 * 根据教师id查询教师信息
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("info/query/{studentId}")
	public String infoQuery(@PathVariable Integer studentId, Model model) {
		StudentInfo studentInfo = studentService.selectById(studentId);
		StudentInfoView student = BeanUtils.copyAs(studentInfo, StudentInfoView.class);
		model.addAttribute("student", student);
		return "teacher/sudentDetail";
	}
	
	/**
	 * 学生信息修改
	 * @param record
	 * @return
	 */
	@RequestMapping("info/update")
	@ResponseBody Object infoUpdate(StudentInfoInputVO vo){
		try {
			StudentInfo student = BeanUtils.copyAs(vo, StudentInfo.class);
			if(StringUtils.isNotBlank(vo.getBirthday())) {
				student.setBirthday(DateUtil.strToDate(vo.getBirthday(), DateUtil.DEFAULT_DATE_FORMAT3));
			}
			studentService.updateById(student);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("----infoUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("inputTotal")
	public String inputTotal() {
		return "student/inputTotal";
	}
	
	@RequestMapping("inputTotal/query")
	@ResponseBody Object inputTotalQuery() {
		try {
			AppUser appUser = getAppUser();
			BaseQueryVO vo = new BaseQueryVO();
			vo.setFinishYear(getCurrentTerm().getFinishYear());
			vo.setSchoolId(appUser.getTeacher().getSchoolId());
			vo.setStudyType(appUser.getTeacher().getStudyType());
			BaseQueryVO queryVO = new BaseQueryVO();
			queryVO.setFinishYear(getCurrentTerm().getFinishYear());
			queryVO.setId(appUser.getTeacher().getId());
			List<TeacherInfoView> teacherInfoView = teacherService.selectClassForTeacher(queryVO);
			if(!StringUtil.isNullOrEmpty(teacherInfoView)) {
				vo.setClazz(teacherInfoView.get(0).getClazz());
				vo.setGrade(String.valueOf(teacherInfoView.get(0).getGrade()));
				vo.setStudyType(teacherInfoView.get(0).getStudyType());
			}
			return studentService.selectInputTotal(vo);
		} catch (Exception e) {
			logger.error("---inputTotalQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping("noInput/list")
	@ResponseBody Object noInputList(BaseQueryVO vo) {
		try {
			AppUser appUser = getAppUser();
			vo.setStudyType(appUser.getStudyType());
			vo.setSchoolId(appUser.getTeacher().getSchoolId());
			vo.setFinishYear(getCurrentTerm().getFinishYear());
			List<StudentInfo> list = studentService.selectNoInputList(vo);
			List<StudentInfoView> viewList = BeanUtils.copyAs(list, StudentInfoView.class);
			return viewList;
		} catch (Exception e) {
			logger.error("---noInputList error={}", e);
		}
		return null;
	}
}
